package loglog

import (
	ctx_h "context"

	"gitee.com/wulalade/hwpack/utils"
	logs "github.com/gogf/gf/v2/os/glog"
)

const (
	LevelError = iota
	LevelWarning
	LevelData
	LevelInformational
	LevelDebug
)

type Handler_t struct {
	debug_t *logs.Logger
	in_t    *logs.Logger
	warn_t  *logs.Logger
	err_t   *logs.Logger
	data_t  *logs.Logger
	lv      int
}

const (
	default_file = "/etc/logs/"
	// default_flags = logs.F_FILE_SHORT | logs.F_TIME_STD
	default_flags = logs.F_FILE_SHORT | logs.F_CALLER_FN | logs.F_TIME_STD
)

var (
	// Handler *logs.BeeLogger
	Handler Handler_t = Handler_t{lv: LevelDebug}
)

func (h *Handler_t) Debug(format string, v ...interface{}) {
	if h.debug_t == nil {
		return
	}
	if h.lv < LevelDebug {
		return
	}
	h.debug_t.Debugf(ctx_h.TODO(), format, v...)
	// h.debug_t.Debugf(format, v...)
}

func (h *Handler_t) Info(format string, v ...interface{}) {
	if h.in_t == nil {
		return
	}
	if h.lv < LevelInformational {
		return
	}
	h.in_t.Infof(ctx_h.TODO(), format, v...)
	// h.in_t.Infof(format, v...)
}

func (h *Handler_t) Data(format string, v ...interface{}) {
	if h.data_t == nil {
		return
	}
	if h.lv < LevelData {
		return
	}
	h.data_t.Infof(ctx_h.TODO(), format, v...)
	// h.data_t.Infof(format, v...)
}

func (h *Handler_t) Warn(format string, v ...interface{}) {
	if h.warn_t == nil {
		return
	}
	if h.lv < LevelWarning {
		return
	}
	h.warn_t.Warningf(ctx_h.TODO(), format, v...)
	// h.warn_t.Warningf(format, v...)
}

func (h *Handler_t) Error(format string, v ...interface{}) {
	if h.err_t == nil {
		return
	}
	if h.lv < LevelError {
		return
	}
	h.err_t.Errorf(ctx_h.TODO(), format, v...)
	// h.err_t.Errorf(format, v...)
}

func init() {

	workPath := utils.GetWorkPath()

	Handler.debug_t = logs.New()
	Handler.debug_t.SetConfigWithMap(map[string]interface{}{
		"path":              workPath + default_file,
		"level":             "all",
		"stdout":            true,
		"file":              "{Y-m-d}_debug.log",
		"StStatus":          0,
		"stSkip":            1,
		"timeFormat":        "2006-01-02 15:04:05.000",
		"rotateSize":        (1 << 21), //2m
		"flags":             default_flags,
		"rotateBackupLimit": 1000,
	})

	Handler.in_t = logs.New()
	Handler.in_t.SetConfigWithMap(map[string]interface{}{
		"path":              workPath + default_file,
		"level":             "all",
		"stdout":            true,
		"file":              "{Y-m-d}_info.log",
		"StStatus":          0,
		"stSkip":            1,
		"timeFormat":        "2006-01-02 15:04:05.000",
		"rotateSize":        (1 << 21), //2m
		"flags":             default_flags,
		"rotateBackupLimit": 1000,
	})

	Handler.warn_t = logs.New()
	Handler.warn_t.SetConfigWithMap(map[string]interface{}{
		"path":              workPath + default_file,
		"level":             "all",
		"stdout":            true,
		"file":              "{Y-m-d}_warn.log",
		"StStatus":          0,
		"stSkip":            1,
		"timeFormat":        "2006-01-02 15:04:05.000",
		"rotateSize":        (1 << 21), //2m
		"flags":             default_flags,
		"rotateBackupLimit": 1000,
	})

	Handler.err_t = logs.New()
	Handler.err_t.SetConfigWithMap(map[string]interface{}{
		"path":              workPath + default_file,
		"level":             "all",
		"stdout":            true,
		"file":              "{Y-m-d}_error.log",
		"StStatus":          0,
		"stSkip":            1,
		"timeFormat":        "2006-01-02 15:04:05.000",
		"rotateSize":        (1 << 21), //2m
		"flags":             default_flags,
		"rotateBackupLimit": 1000,
	})

	Handler.data_t = logs.New()
	Handler.data_t.SetConfigWithMap(map[string]interface{}{
		"path":              workPath + default_file,
		"level":             "all",
		"stdout":            true,
		"file":              "{Y-m-d}_data.log",
		"StStatus":          0,
		"stSkip":            1,
		"timeFormat":        "2006-01-02 15:04:05.000",
		"rotateSize":        (1 << 21), //2m
		"flags":             default_flags,
		"rotateBackupLimit": 1000,
	})

	Handler.Info("log init in %s successful", workPath+default_file)

}

func SetMaxsize(Maxsize int) {
	if Handler.debug_t != nil {
		Handler.debug_t.SetConfigWithMap(map[string]interface{}{
			"rotateSize": Maxsize,
		})
	}
	if Handler.data_t != nil {
		Handler.data_t.SetConfigWithMap(map[string]interface{}{
			"rotateSize": Maxsize,
		})
	}
	if Handler.in_t != nil {
		Handler.in_t.SetConfigWithMap(map[string]interface{}{
			"rotateSize": Maxsize,
		})
	}
	if Handler.err_t != nil {
		Handler.err_t.SetConfigWithMap(map[string]interface{}{
			"rotateSize": Maxsize,
		})
	}
	if Handler.warn_t != nil {
		Handler.warn_t.SetConfigWithMap(map[string]interface{}{
			"rotateSize": Maxsize,
		})
	}
}

func SetLevel(l int) {
	Handler.lv = l
}
